// code-order/pages/invoices.js
import router from '@src/router'
import {
    findInvoiceTab
} from "@src/api/invoice";
import invoiceBehavior from "../invoice/invoiceBehavior";

Page({
    behaviors: [invoiceBehavior],
    data: {
        currentIndex: 0, //默认高亮
        colorMap: {
            1: '#4AB759',
            2: '#FFB72F',
            3: '#F05357',
            4: '#FFB72F',
            5: '#F05357'
        }
    },
    async onLoad(options) {
        const flag = await this.findInvoiceTab()
        options.currentIndex && this.setData({
            currentIndex: options.currentIndex
        })
        const {
            slots,
            currentIndex
        } = this.data
        flag && options.currentIndex === '0' && this.tabChangeApi(slots[currentIndex].orderType)

    },
    findInvoiceTab() {
        return new Promise(resolve => {
            findInvoiceTab().then(res => {
                if (res.code === 0) {
                    const slotsFilter = this.data.slots.slice()
                    const slots = slotsFilter.filter(item => {
                        item.flag = res.data[item.responseSts]
                        return item.flag === 1
                    })
                    this.setData({
                        slots,
                        loading: false
                    })
                    resolve(true)
                }
            })
        })
    },
   
    changeTab(e) {
        const {
            slots,
        } = this.data
        const orderType = slots[e.detail].orderType
        this.data.currentIndex = e.detail
        this.data.orderType = orderType
        !slots[e.detail].orderData.length && this.tabChangeApi(orderType)
    },
    goDetails(e) {
        const {
            slots,
            currentIndex,
            orderType
        } = this.data
        const {
            index
        } = e.currentTarget.dataset
        let key = slots[currentIndex].orderData[index]
        key.currentIndex = currentIndex
        router.push({
            path: 'code-order/pages/invoicDetails/main',
            success: (res) => {
                res.eventChannel.emit('key', key)
            }
        })
    },

    goInvoiceExplain() {
        router.push({
            path: 'code-order/pages/invoiceExplain/main'
        })
    },

})